﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ResDispense
{
    public partial class Form1 : Form
    {
        //创建各线程对象
        private Thread startWorkThread;

        //当前执行的任务编号
        private int currentTaskID = 0;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// startWorkBtn_Click 点击startWorkBtn后的事件
        /// </summary>
        private void startWorkBtn_Click(object sender, EventArgs e)
        {
            if (this.startWorkBtn.Text == "开始")
            {
                this.startWorkThread = new Thread(new ThreadStart(this.scan_res));
                this.startWorkThread.IsBackground = true;
                this.startWorkThread.Start();

                this.startWorkBtn.Text = "停止";
            }
            else
            {
                this.startWorkThread.Abort();
                this.startWorkBtn.Text = "开始";
            }
        }

        /// <summary>
        /// 执行主抓取工作
        /// </summary>
        private void scan_res()
        {
            this.set_toolStripStatusLabel1_text("准备...");

            MysqlDB mysqldb = new MysqlDB();
            mysqldb.DBConnection();

            //如果当前没有执行任务,则应该去取得新任务
            if (currentTaskID == 0)
            {
                this.set_toolStripStatusLabel1_text("正在从主服务器申请任务...");

                //从数据库中取得任务
                TaskClass  taskClass  = new TaskClass();
                TaskStruct taskstruct = taskClass.GetNewTask(mysqldb);
                int sourceid = taskstruct.GetSourceID();

                if (sourceid > 0)
                {
                    this.set_toolStripStatusLabel1_text("已经取得一条任务,正在解析任务...");

                    //根据任务中的"读取来源"进行处理
                    SourceClass sourceClass = new SourceClass();
                    SourceStruct sourceStruct = sourceClass.GetSourceInfoByID(sourceid, mysqldb);

                    int source_id = sourceStruct.GetSourceID();                         //来源ID
                    string url = sourceStruct.GetUrl();                                 //抓取来源url
                    string match_function = sourceStruct.GetMatchFunction();            //抓取器名称
                    string filter_function = sourceStruct.GetFilterFunction();          //过滤器名称
                    string class_code = sourceStruct.GetClassCode();                    //所属文章类别

                    MessageBox.Show("source_id =" + source_id.ToString() + "|||url =" + url + "|||match_function =" + match_function + "|||filter_function =" + filter_function + "|||class_code =" + class_code);
                }
                else
                {
                    this.set_toolStripStatusLabel1_text("暂时没有可执行的任务.");
                }
            }

            mysqldb.DBClose();
        }

        public void set_toolStripStatusLabel1_text(string text)
        {
            try
            {
                this.toolStripStatusLabel1.Text = text;
            }
            catch (Exception)
            {
            }
        }
    }
}